<template>
    <div class="novel">
        <el-card>
            <el-form :inline="true" :model="search" class="demo-form-inline">
                <el-form-item label="名称">
                    <el-input v-model="search.name" placeholder="搜索"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" @click="novelSearch"></el-button>
                    <el-select @change="loadNovel()" v-model="search.category" placeholder="按类查询"
                               style="margin-left: 50px">
                        <el-option :label="c.genre" :value="c.genre" v-for="c in categories"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-button type="primary" @click="dialogFormVisible = true">添加</el-button>

            <!--            添加小说的表格-->
            <el-dialog title="添加小说" :visible.sync="dialogFormVisible">
                <el-form :model="insert">
                    <el-form-item label="小说名字" :label-width="'120px'">
                        <el-input style="width:100px;" size="medium" v-model="insert.name"
                                  autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="作者" :label-width="'120px'">
                        <el-input style="width:100px;" size="medium" v-model="insert.author"
                                  autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="简介" :label-width="'120px'">
                        <el-input type="textarea" v-model="insert.description" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="章节数" :label-width="'120px'">
                        <el-input style="width:100px;" size="medium" v-model="insert.chapter_count"
                                  autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="类别" :label-width="'120px'">
                        <el-select v-model="insert.category" placeholder="选择类别">
                            <el-option :label="c.genre" :value="c.genre" v-for="c in categories"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>


                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="novelAdd">确 定</el-button>
                </div>
            </el-dialog>

            <el-table :data="novels.list">
                <el-table-column label="小说名字">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.name" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="作者">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.author" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.author}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="简介">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.description" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.description}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="类别">
                    <template scope="scope">
                        <!--                        <el-input v-if="scope.row.status" size="small" v-model="temp.category" placeholder="请输入内容"-->
                        <!--                        ></el-input>-->
                        <el-select v-if="scope.row.status" v-model="temp.category" placeholder="选择类别"
                                   style="margin-left: 50px">
                            <el-option :label="c.genre" :value="c.genre" v-for="c in categories"></el-option>
                        </el-select>
                        <span v-else>{{scope.row.category}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="章节数">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.chapter_count" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.chapter_count}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="阅读地址">
                    <template scope="scope">
                        <el-input v-if="scope.row.status" size="small" v-model="temp.href" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.href}}</span>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.status" @click="confirmEdit(scope.$index,scope.row)" type="text"
                                   size="small">确认
                        </el-button>
                        <el-button v-else type="text" size="small" @click="novelDelete(scope.row)">删除</el-button>
                        <el-button v-if="scope.row.status" type="text" size="small"
                                   @click="cancelEdit(scope.$index, scope.row)">取消编辑
                        </el-button>
                        <el-button type="text" size="small" @click="edit(scope.$index, scope.row)" v-else>编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    background
                    layout="prev, pager, next"
                    :total="novels.pages"
                    :current-page.sync="currentPage"
                    @current-change="handleCurrentChange">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                novels: {},
                temp: [],
                currentPage: 1,
                search: {name: "", category: ""},
                insert: {},
                dialogTableVisible: false,
                dialogFormVisible: false,
                categories: ""
            }
        },
        mounted() {
            this.loadNovel()
            this.$http.get("/genres", {params: {big_genre: '小说'}}).then((res) => {
                this.categories = res.data.data
            })
        },
        methods: {
            loadNovel() {
                this.$http.get("/novel/byCate", {
                    params: {
                        start: this.currentPage,
                        category: this.search.category
                    }
                }).then((res) => {
                    if (res.data.code == 0) {
                        this.novels = res.data.data
                        this.novels.list.forEach((x) => {
                            x.status = false;
                        })
                    }
                })
            },
            novelSearch() {
                this.$http.post("/novel/byName", {name: this.search.name}).then((res) => {
                    if (res.data.code == 0) {
                        this.novels.list = res.data.data
                        this.novels.pages = 1
                        this.novels.list.forEach((x) => {
                            x.status = false;
                        })
                    }
                })
            },
            handleCurrentChange() {
                this.loadNovel()
            },
            handleEdit(index, row) {
                console.log(index, row);
                row.status = false;
                this.$set(this.novels, index, row)
            },
            novelDelete(row) {
                this.$http.post("/novel/delete", row).then((res) => {
                    if (res.data.code == 0) {
                        this.$msg.success(res.data.message + '请刷新页面')
                    } else {
                        this.$msg.fail(res.data.message)
                    }
                })
            },
            edit(index, row) {
                this.temp = JSON.parse(JSON.stringify(row))
                row.status = true;
                this.$set(this.novels.list, index, row)
            },
            cancelEdit(index, row) {
                row.status = false;
                this.$set(this.novels.list, index, row)
            },
            confirmEdit(index, row) {
                row = JSON.parse(JSON.stringify(this.temp))
                this.$set(this.novels.list, index, JSON.parse(JSON.stringify(this.temp)))
                row.status = false;
                this.$http.post("/novel/update", this.temp).then((res)=>{
                    this.$msg.success(res.data.message)
                })
            },
            novelAdd() {
                this.$http.post("/novel/insert", this.insert).then((res) => {
                    if (res.data.code == 0) {
                        this.dialogFormVisible = false;
                        this.$msg.success(res.data.message)
                    } else {
                        this.$msg.fail(res.data.message)
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>